package cn.wikiflyer.beikang.act.user.setting;

import java.lang.reflect.Type;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.IApplication;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wikiflyer.beikang.utils.task.GetVerifyCodeTask;
import cn.wk.libs4a.net.WKAsyncTaskPro;
import cn.wk.libs4a.utils.WKCountDownUtils;

/** @author maizi
 * UserSettingModifyPswAct.java
 * 2015年6月1日
 * 修改密码
 */
public class UserSettingModifyPswAct extends BaseActivity {
	
	@ViewInject(id = R.id.et_pass_old)
	private EditText et_pass_old;
	@ViewInject(id = R.id.et_pass_new1)
	private EditText et_pass_new1;
	@ViewInject(id = R.id.et_pass_new2)
	private EditText et_pass_new2;
	@ViewInject(id = R.id.et_get_code)
	private EditText et_get_code;
	@ViewInject(id = R.id.tv_get_code)
	private Button tv_getCode;
	@ViewInject(id = R.id.modify_commit)
	private Button btn_submit;
	private WKCountDownUtils cdUtils = null;

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_user_modify_pwd);
	}

	@Override
	public void init() {
		cdUtils = new WKCountDownUtils(tv_getCode, "获取验证码", 600, 1);
		btn_submit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (checkCorrect()) {
					new ModifyPassTask(ctx);
				}
			}
		});
		tv_getCode.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				cdUtils.start();
				new GetVerifyCodeTask(ctx, ""+app().getLoginSession().username);
			}
		});
	}

	@Override
	public void setData() {

	}
	/** 检查两次输入密码是否一致
	 */
	private boolean checkCorrect(){
		if (TextUtils.isEmpty(et_pass_old.getText().toString())||TextUtils.isEmpty(et_pass_new1.getText().toString())||TextUtils.isEmpty(et_pass_new2.getText().toString())) {
			Toast.makeText(ctx, "密码不能为空", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (TextUtils.isEmpty(et_get_code.getText().toString())) {
			Toast.makeText(ctx, "验证码不能为空", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (et_pass_new1.getText().toString().equals(et_pass_new2.getText().toString())) {
			return true;
		}
		Toast.makeText(ctx, "两次输入密码不一致", Toast.LENGTH_SHORT).show();
		return false;
	}
	
	/** @author maizi
	 * UserSettingModifyPswAct.java
	 * 修改密码线程
	 */
	class ModifyPassTask extends WKAsyncTaskPro{
		public ModifyPassTask(Context context) {
			super(context);
		}
		@Override
		protected String doInBackground(boolean... params) {
			return app().net.modifyPassWord(et_pass_old.getText().toString(),et_pass_new1.getText().toString(),et_get_code.getText().toString());
		}
		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean base = gson.fromJson(result, cvbType);
			if (base.status == 1) {
				Toast.makeText(ctx, "修改成功", Toast.LENGTH_SHORT).show();
				finish();
			}else{
				Toast.makeText(ctx, "修改失败", Toast.LENGTH_SHORT).show();
			}
		}
	}

}
